/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.generatepage.web.GenerateContentAction
   Module Description   :

   Date Created      : 2009/12/29
   Original Author   : feeling.wang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.generatepage.web;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.text.ParseException;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.ec.category.entity.Category;
import com.yaodian100.ec.category.service.CategoryService;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.generate.entity.EventImage;
import com.yaodian100.ec.generate.entity.GenerateAdvertise;
import com.yaodian100.ec.generate.entity.GenerateContent;
import com.yaodian100.ec.generate.entity.GeneratePosition;
import com.yaodian100.ec.generate.service.GenerateService;
import com.yaodian100.ec.product.entity.Product;
import com.yaodian100.ec.product.service.ProductService;
import com.yaodian100.ec.shelf.service.ShelfService;

/**
 * @author feeling.wang
 * 
 */
public class CateMainProdAdvAction extends ActionSupport implements Preparable {
	private static final long serialVersionUID = -9077554124798942202L;

	/** logger */
	private final Logger logger = LoggerFactory.getLogger(CateMainProdAdvAction.class);
	private GenerateService generateService;
	private CategoryService categoryService;
	private DomainService<Menu> menuService;
	private ProductService productService;
	private ShelfService shelfService;
	private Product jsonProduct;
	private String productId;
	private Menu advStatusMenu; // 與 getter, setter
	private Menu advTypeMenu; // 與 getter, setter
	private Menu advBannerTagMenu;
	private List<Category> categoryRoot;
	private List<GenerateAdvertise> advertises;
	private GenerateAdvertise advertise;
	private GeneratePosition position;
	private GenerateContent content;
	private SimplePager pager;
	private String advStatus;// 00:草稿,10:发送,90:取消
	private String advId;
	private String advName;
	private String advPosition;// 位置
	private String startDate;// 开始时间
	private String endDate;// 结束时间
	private String mainCateId;
	private String advType;
	private String childCateId;
	private String advTarget;
	private String tab1Title;
	private String tab1ProdId1;
	private String tab1ProdId2;
	private String tab1ProdId3;
	private String tab2Title;
	private String tab2ProdId1;
	private String tab2ProdId2;
	private String tab2ProdId3;
	private String tab3Title;
	private String tab3ProdId1;
	private String tab3ProdId2;
	private String tab3ProdId3;
	private String tab4Title;
	private String tab4ProdId1;
	private String tab4ProdId2;
	private String tab4ProdId3;
	private String tab5Title;
	private String tab5ProdId1;
	private String tab5ProdId2;
	private String tab5ProdId3;
	private StringBuilder contents;

	private File bannerImg;
	private String bannerImgLink;
	private String bannerImgName;
	private String bannerImgFolder;

	private String bannerTag1;
	private String bannerContent1;
	private String bannerLink1;

	private String bannerTag2;
	private String bannerContent2;
	private String bannerLink2;

	private String bannerTag3;
	private String bannerContent3;
	private String bannerLink3;

	private EventImage eventImage = null;

	/******* 查询页面参数 ********/
	private String advNameParam;
	private String startDateParam;
	private String endDateParam;
	private String advStatusParam;
	private String advTypeParam;

	private final String lineBreak = "\n";

	public String addAdv() throws Exception {
		return "addAdv";
	}

	/**
	 * 新增館首頁主打商品banner
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addCateBannerAdv() throws Exception {
		initVariables();
		advId = "";
		return "editCateBannerAdv";
	}

	/**
	 * 新增館首頁/館類別主打商品
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addCateMainProdAdv() throws Exception {
		initVariables();
		advId = "";
		return "editCateMainProdAdv";
	}

	/**
	 * 館首頁主打商品banner欄位驗證
	 * 
	 * @return
	 * @throws CoreException
	 * @throws ParseException
	 */
	private boolean cateBannerAdvValidate() throws CoreException, ParseException {
		/**************************** 必填栏位 ****************************/
		if (StringUtils.isBlank(advName)) {
			addFieldError("advName", "请输入广告名称");
		}

		if (StringUtils.isBlank(advType)) {
			addFieldError("advType", "请选择广告类型");
		}

		if (StringUtils.isBlank(startDate)) {
			addFieldError("startDate", "请输入起始日期");
		}

		if (StringUtils.isBlank(endDate)) {
			addFieldError("endDate", "请输入结束日期");
		}

		if (StringUtils.isBlank(mainCateId)) {
			addFieldError("mainCateId", "请选择馆别");
		}

		if (StringUtils.isBlank(advTarget)) {
			addFieldError("advTarget", "请选择位置");
		}

		if (StringUtils.isBlank(bannerImgName)) {
			addFieldError("bannerImgName", "請選擇bannerImg");
		}

		if (StringUtils.isBlank(bannerImgLink)) {
			addFieldError("bannerImgLink", "請輸入bannerImgLink");
		}

		if (StringUtils.isBlank(bannerTag1)) {
			addFieldError("bannerTag1", "請輸入bannerTag1");
		}

		if (StringUtils.isBlank(bannerTag2)) {
			addFieldError("bannerTag2", "請輸入bannerTag2");
		}

		if (StringUtils.isBlank(bannerTag3)) {
			addFieldError("bannerTag2", "請輸入bannerTag3");
		}

		if (StringUtils.isBlank(bannerContent1)) {
			addFieldError("bannerContent1", "請輸入bannerContent1");
		}

		if (StringUtils.isBlank(bannerContent2)) {
			addFieldError("bannerContent2", "請輸入bannerContent2");
		}

		if (StringUtils.isBlank(bannerContent3)) {
			addFieldError("bannerContent3", "請輸入bannerContent3");
		}

		if (StringUtils.isBlank(bannerLink1)) {
			addFieldError("bannerLink1", "請輸入bannerLink1");
		}

		if (StringUtils.isBlank(bannerLink2)) {
			addFieldError("bannerLink2", "請輸入bannerLink2");
		}

		if (StringUtils.isBlank(bannerLink3)) {
			addFieldError("bannerLink3", "請輸入bannerLink3");
		}

		if (getFieldErrors().isEmpty()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 欄位驗證
	 * 
	 * @return
	 * @throws CoreException
	 * @throws ParseException
	 */
	private boolean cateMainProdAdvValidate() throws CoreException, ParseException {
		/**************************** 必填栏位 ****************************/
		if (StringUtils.isBlank(advName)) {
			addFieldError("advName", "请输入广告名称");
		}

		if (StringUtils.isBlank(advType)) {
			addFieldError("advType", "请选择广告类型");
		}

		if (StringUtils.isBlank(startDate)) {
			addFieldError("startDate", "请输入起始日期");
		}

		if (StringUtils.isBlank(endDate)) {
			addFieldError("endDate", "请输入结束日期");
		}

		if (StringUtils.isBlank(mainCateId)) {
			addFieldError("mainCateId", "请选择馆别");
		}

		if (StringUtils.equalsIgnoreCase("100_PROMOTION", advType)) {
			if (StringUtils.isBlank(childCateId)) {
				addFieldError("childCateId", "请选择次分类");
			}
		}

		if (StringUtils.isBlank(advTarget)) {
			addFieldError("advTarget", "请选择位置");
		}

		if (StringUtils.isBlank(tab1Title)) {
			addFieldError("tab1Title", "請輸入tab1 title");
		}

		if (StringUtils.isBlank(tab2Title)) {
			addFieldError("tab2Title", "請輸入tab2 title");
		}

		if (StringUtils.isBlank(tab3Title)) {
			addFieldError("tab3Title", "請輸入tab3 title");
		}

		if (StringUtils.isBlank(tab4Title)) {
			addFieldError("tab4Title", "請輸入tab4 title");
		}

		if (StringUtils.isBlank(tab5Title)) {
			addFieldError("tab5Title", "請輸入tab5 title");
		}

		if (StringUtils.isBlank(tab1ProdId1)) {
			addFieldError("tab1ProdId1", "請輸入tab1 ProdId1");
		}

		if (StringUtils.isBlank(tab1ProdId2)) {
			addFieldError("tab1ProdId2", "請輸入tab1 ProdId2");
		}

		if (StringUtils.isBlank(tab1ProdId3)) {
			addFieldError("tab1ProdId3", "請輸入tab1 ProdId3");
		}

		if (StringUtils.isBlank(tab2ProdId1)) {
			addFieldError("tab2ProdId1", "請輸入tab2 ProdId1");
		}

		if (StringUtils.isBlank(tab2ProdId2)) {
			addFieldError("tab2ProdId2", "請輸入tab2 ProdId2");
		}

		if (StringUtils.isBlank(tab2ProdId3)) {
			addFieldError("tab2ProdId3", "請輸入tab2 ProdId3");
		}

		if (StringUtils.isBlank(tab3ProdId1)) {
			addFieldError("tab3ProdId1", "請輸入tab3 ProdId1");
		}

		if (StringUtils.isBlank(tab3ProdId2)) {
			addFieldError("tab3ProdId2", "請輸入tab3 ProdId2");
		}

		if (StringUtils.isBlank(tab3ProdId3)) {
			addFieldError("tab3ProdId3", "請輸入tab3 ProdId3");
		}

		if (StringUtils.isBlank(tab4ProdId1)) {
			addFieldError("tab4ProdId1", "請輸入tab4 ProdId1");
		}

		if (StringUtils.isBlank(tab4ProdId2)) {
			addFieldError("tab4ProdId2", "請輸入tab4 ProdId2");
		}

		if (StringUtils.isBlank(tab4ProdId3)) {
			addFieldError("tab4ProdId3", "請輸入tab4 ProdId3");
		}

		if (StringUtils.isBlank(tab5ProdId1)) {
			addFieldError("tab5ProdId1", "請輸入tab5 ProdId1");
		}

		if (StringUtils.isBlank(tab5ProdId2)) {
			addFieldError("tab5ProdId2", "請輸入tab5 ProdId2");
		}

		if (StringUtils.isBlank(tab5ProdId3)) {
			addFieldError("tab5ProdId3", "請輸入tab5 ProdId3");
		}

		if (getFieldErrors().isEmpty()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 编辑館首頁打商品banner
	 * 
	 * @return
	 * @throws Exception
	 */
	public String editCateBannerAdv() throws Exception {
		initVariables();
		advertise = generateService.get(Long.parseLong(advId));
		position = generateService.getGeneratePositionByAdvId(Long.parseLong(advId)).get(0);
		content = generateService.getGenerateContentByAdvId(Long.parseLong(advId)).get(0);
		readPropertise(advertise.getAdvType(), content);
		advType = advertise.getAdvType();
		advName = advertise.getAdvName();
		mainCateId = position.getMainCateId().toString();
		if (advertise.getAdvType().equals("100_BANNER")) {
			childCateId = position.getChildCateId().toString();
		}
		advTarget = position.getAdvTarget();
		startDate = FastDateFormat.getInstance(EcConstant.PATTERN_DATE).format(advertise.getStartDate());
		endDate = FastDateFormat.getInstance(EcConstant.PATTERN_DATE).format(advertise.getEndDate());
		return "editCateBannerAdv";
	}

	/**
	 * 编辑館首頁/分類頁主打商品资料
	 * 
	 * @return
	 * @throws Exception
	 */
	public String editCateMainProdAdv() throws Exception {
		initVariables();
		advertise = generateService.get(Long.parseLong(advId));
		position = generateService.getGeneratePositionByAdvId(Long.parseLong(advId)).get(0);
		content = generateService.getGenerateContentByAdvId(Long.parseLong(advId)).get(0);
		advType = advertise.getAdvType();
		advName = advertise.getAdvName();
		mainCateId = position.getMainCateId().toString();
		advTarget = position.getAdvTarget();
		startDate = FastDateFormat.getInstance(EcConstant.PATTERN_DATE).format(advertise.getStartDate());
		endDate = FastDateFormat.getInstance(EcConstant.PATTERN_DATE).format(advertise.getEndDate());
		if (advertise.getAdvType().equals("100_PROMOTION")) {
			childCateId = position.getChildCateId().toString();
		}
		readPropertise(advertise.getAdvType(), content);
		return "editCateMainProdAdv";
	}

	/**
	 * 查询商品名称
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findProductName() throws Exception {
		if (StringUtils.isNotBlank(productId)) {
			jsonProduct = productService.get(productId);
			if (null != jsonProduct) {
				jsonProduct.setProductBrief(shelfService.getActiveShelfByProductId(productId).getPromoteName());
			}
		}
		return "jsonFindProductName";
	}

	/**
	 * 查询商品是否可上广告
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findProductData() throws Exception {
		if (StringUtils.isNotBlank(productId)) {
			jsonProduct = productService.get(productId);
			if (null != jsonProduct) {
				if (null == shelfService.getActiveShelfByProductId(productId)) {
					jsonProduct.setProductBrief("商品已下架");
				} else {
					jsonProduct.setProductBrief(shelfService.getActiveShelfByProductId(productId).getPromoteName());
				}
			}
		}
		return "jsonFindProductData";
	}

	public Menu getAdvBannerTagMenu() {
		return advBannerTagMenu;
	}

	public GenerateAdvertise getAdvertise() {
		return advertise;
	}

	public List<GenerateAdvertise> getAdvertises() {
		return advertises;
	}

	public String getAdvId() {
		return advId;
	}

	public String getAdvName() {
		return advName;
	}

	public String getAdvNameParam() {
		return advNameParam;
	}

	public String getAdvPosition() {
		return advPosition;
	}

	public String getAdvStatus() {
		return advStatus;
	}

	public Menu getAdvStatusMenu() {
		return advStatusMenu;
	}

	public String getAdvStatusParam() {
		return advStatusParam;
	}

	public String getAdvTarget() {
		return advTarget;
	}

	public String getAdvType() {
		return advType;
	}

	public Menu getAdvTypeMenu() {
		return advTypeMenu;
	}

	public String getAdvTypeParam() {
		return advTypeParam;
	}

	public String getBannerContent1() {
		return bannerContent1;
	}

	public String getBannerContent2() {
		return bannerContent2;
	}

	public String getBannerContent3() {
		return bannerContent3;
	}

	public File getBannerImg() {
		return bannerImg;
	}

	public String getBannerImgFolder() {
		return bannerImgFolder;
	}

	public String getBannerImgLink() {
		return bannerImgLink;
	}

	public String getBannerImgName() {
		return bannerImgName;
	}

	public String getBannerLink1() {
		return bannerLink1;
	}

	public String getBannerLink2() {
		return bannerLink2;
	}

	public String getBannerLink3() {
		return bannerLink3;
	}

	public String getBannerTag1() {
		return bannerTag1;
	}

	public String getBannerTag2() {
		return bannerTag2;
	}

	public String getBannerTag3() {
		return bannerTag3;
	}

	public List<Category> getCategoryRoot() {
		return categoryRoot;
	}

	public String getChildCateId() {
		return childCateId;
	}

	public GenerateContent getContent() {
		return content;
	}

	public StringBuilder getContents() {
		return contents;
	}

	public String getEndDate() {
		return endDate;
	}

	public String getEndDateParam() {
		return endDateParam;
	}

	public Product getJsonProduct() {
		return jsonProduct;
	}

	public String getMainCateId() {
		return mainCateId;
	}

	public SimplePager getPager() {
		return pager;
	}

	public GeneratePosition getPosition() {
		return position;
	}

	public String getProductId() {
		return productId;
	}

	public ProductService getProductService() {
		return productService;
	}

	public String getStartDate() {
		return startDate;
	}

	public String getStartDateParam() {
		return startDateParam;
	}

	public String getTab1ProdId1() {
		return tab1ProdId1;
	}

	public String getTab1ProdId2() {
		return tab1ProdId2;
	}

	public String getTab1ProdId3() {
		return tab1ProdId3;
	}

	public String getTab1Title() {
		return tab1Title;
	}

	public String getTab2ProdId1() {
		return tab2ProdId1;
	}

	public String getTab2ProdId2() {
		return tab2ProdId2;
	}

	public String getTab2ProdId3() {
		return tab2ProdId3;
	}

	public String getTab2Title() {
		return tab2Title;
	}

	public String getTab3ProdId1() {
		return tab3ProdId1;
	}

	public String getTab3ProdId2() {
		return tab3ProdId2;
	}

	public String getTab3ProdId3() {
		return tab3ProdId3;
	}

	public String getTab3Title() {
		return tab3Title;
	}

	public String getTab4ProdId1() {
		return tab4ProdId1;
	}

	public String getTab4ProdId2() {
		return tab4ProdId2;
	}

	public String getTab4ProdId3() {
		return tab4ProdId3;
	}

	public String getTab4Title() {
		return tab4Title;
	}

	public String getTab5ProdId1() {
		return tab5ProdId1;
	}

	public String getTab5ProdId2() {
		return tab5ProdId2;
	}

	public String getTab5ProdId3() {
		return tab5ProdId3;
	}

	public String getTab5Title() {
		return tab5Title;
	}

	/**
	 * 初始化变数值
	 */
	void initVariables() {
		advertise = new GenerateAdvertise();
		position = new GeneratePosition();
		content = new GenerateContent();

		advStatus = "";
		advName = "";
		advPosition = "";
		startDate = "";
		endDate = "";
		mainCateId = "";
		// advType = "";
		childCateId = "";
		advTarget = "";
		tab1Title = "";
		tab1ProdId1 = "";
		tab1ProdId2 = "";
		tab1ProdId3 = "";
		tab2Title = "";
		tab2ProdId1 = "";
		tab2ProdId2 = "";
		tab2ProdId3 = "";
		tab3Title = "";
		tab3ProdId1 = "";
		tab3ProdId2 = "";
		tab3ProdId3 = "";
		tab4Title = "";
		tab4ProdId1 = "";
		tab4ProdId2 = "";
		tab4ProdId3 = "";
		tab5Title = "";
		tab5ProdId1 = "";
		tab5ProdId2 = "";
		tab5ProdId3 = "";
		bannerImg = null;
		bannerImgLink = "";
		bannerImgName = "";
		bannerImgFolder = "";

		bannerTag1 = "";
		bannerContent1 = "";
		bannerLink1 = "";

		bannerTag2 = "";
		bannerContent2 = "";
		bannerLink2 = "";

		bannerTag3 = "";
		bannerContent3 = "";
		bannerLink3 = "";

		startDate = "";
		endDate = "";
	}

	public void prepare() throws Exception {
		clearErrorsAndMessages();

		if (null == pager) {
			pager = new SimplePager();
		}
		pager.setPageRecord(20);// defaut 20

		if (null == categoryRoot) {
			categoryRoot = categoryService.getChild(0l);
		}

		if (null == advStatusMenu) {
			advStatusMenu = menuService.get("Generate.AdvStatus");
		}

		if (null == advTypeMenu) {
			advTypeMenu = menuService.get("Generate.AdvType");
		}

		if (null == advBannerTagMenu) {
			advBannerTagMenu = menuService.get("Generate.AdvBannerTag");
		}
		logger.info("prepare ok");
	}

	/**
	 * 從db讀取馆首页/分类页主打商品、banner的propertise資料
	 * 
	 * @param type
	 * @param content
	 * @throws IOException
	 */
	void readPropertise(String type, GenerateContent content) throws IOException {
		Properties prop = new Properties();
		Reader reader = new StringReader(content.getContents());
		prop.load(reader);
		reader.close();
		if (StringUtils.equalsIgnoreCase("000_BANNER", type) || StringUtils.equalsIgnoreCase("100_BANNER", type)) {
			bannerImgName = StringUtils.defaultString(prop.get("bannerImgName").toString(), "");
			bannerImgLink = StringUtils.defaultString(prop.get("bannerImgLink").toString(), "");
			bannerImgFolder = StringUtils.defaultString(prop.get("bannerImgFolder").toString(), "");

			bannerTag1 = StringUtils.defaultString(prop.get("bannerTag1").toString(), "");
			bannerContent1 = StringUtils.defaultString(prop.get("bannerContent1").toString(), "");

			bannerLink1 = StringUtils.defaultString(prop.get("bannerLink1").toString(), "");

			bannerTag2 = StringUtils.defaultString(prop.get("bannerTag2").toString(), "");
			bannerContent2 = StringUtils.defaultString(prop.get("bannerContent2").toString(), "");
			bannerLink2 = StringUtils.defaultString(prop.get("bannerLink2").toString(), "");

			bannerTag3 = StringUtils.defaultString(prop.get("bannerTag3").toString(), "");
			bannerContent3 = StringUtils.defaultString(prop.get("bannerContent3").toString(), "");
			bannerLink3 = StringUtils.defaultString(prop.get("bannerLink3").toString(), "");

		} else {
			tab1Title = StringUtils.defaultString(prop.get("tab1Title").toString(), "");
			tab1ProdId1 = StringUtils.defaultString(prop.get("tab1ProdId1").toString(), "");
			tab1ProdId2 = StringUtils.defaultString(prop.get("tab1ProdId2").toString(), "");
			tab1ProdId3 = StringUtils.defaultString(prop.get("tab1ProdId3").toString(), "");

			tab2Title = StringUtils.defaultString(prop.get("tab2Title").toString(), "");
			tab2ProdId1 = StringUtils.defaultString(prop.get("tab2ProdId1").toString(), "");
			tab2ProdId2 = StringUtils.defaultString(prop.get("tab2ProdId2").toString(), "");
			tab2ProdId3 = StringUtils.defaultString(prop.get("tab2ProdId3").toString(), "");

			tab3Title = StringUtils.defaultString(prop.get("tab3Title").toString(), "");
			tab3ProdId1 = StringUtils.defaultString(prop.get("tab3ProdId1").toString(), "");
			tab3ProdId2 = StringUtils.defaultString(prop.get("tab3ProdId2").toString(), "");
			tab3ProdId3 = StringUtils.defaultString(prop.get("tab3ProdId3").toString(), "");

			tab4Title = StringUtils.defaultString(prop.get("tab4Title").toString(), "");
			tab4ProdId1 = StringUtils.defaultString(prop.get("tab4ProdId1").toString(), "");
			tab4ProdId2 = StringUtils.defaultString(prop.get("tab4ProdId2").toString(), "");
			tab4ProdId3 = StringUtils.defaultString(prop.get("tab4ProdId3").toString(), "");

			tab5Title = StringUtils.defaultString(prop.get("tab5Title").toString(), "");
			tab5ProdId1 = StringUtils.defaultString(prop.get("tab5ProdId1").toString(), "");
			tab5ProdId2 = StringUtils.defaultString(prop.get("tab5ProdId2").toString(), "");
			tab5ProdId3 = StringUtils.defaultString(prop.get("tab5ProdId3").toString(), "");
		}
	}

	/**
	 * @return
	 * @throws Exception
	 */
	public String saveCateBannerAdv() throws Exception {
		boolean result = false;
		String today = FastDateFormat.getInstance(EcConstant.DEFAULT_PATTERN_DATE).format(DateUtil.getToday().getTime());

		if (cateBannerAdvValidate()) {
			contents = new StringBuilder("");
			// upload file
			if (GenerateHelper.mkDir(advType)) {
				if (StringUtils.isBlank(advId)) {// add
					eventImage = saveImgFile(bannerImg, bannerImgName, advType);
					if (null != eventImage) {
						contents.append("bannerImgName=").append(eventImage.getUuid()).append(eventImage.getImgType()).append(
								lineBreak);
						contents.append("bannerImgFolder=").append(today).append(lineBreak);
					}
				} else {// update
					if (null == bannerImg) {
						contents.append("bannerImgName=").append(bannerImgName).append(lineBreak);
						contents.append("bannerImgFolder=").append(bannerImgFolder).append(lineBreak);
					} else {
						eventImage = saveImgFile(bannerImg, bannerImgName, advType);
						if (null != eventImage) {
							contents.append("bannerImgName=").append(eventImage.getUuid()).append(eventImage.getImgType()).append(
									lineBreak);
							contents.append("bannerImgFolder=").append(today).append(lineBreak);
						}
					}
				}
			}
			contents.append("bannerImgLink=").append(bannerImgLink).append(lineBreak);
			contents.append("bannerTag1=").append(bannerTag1).append(lineBreak);
			contents.append("bannerContent1=").append(bannerContent1).append(lineBreak);
			contents.append("bannerLink1=").append(bannerLink1).append(lineBreak);
			contents.append("bannerTag2=").append(bannerTag2).append(lineBreak);
			contents.append("bannerContent2=").append(bannerContent2).append(lineBreak);
			contents.append("bannerLink2=").append(bannerLink2).append(lineBreak);
			contents.append("bannerTag3=").append(bannerTag3).append(lineBreak);
			contents.append("bannerContent3=").append(bannerContent3).append(lineBreak);
			contents.append("bannerLink3=").append(bannerLink3).append(lineBreak);

			if (StringUtils.isNotBlank(advId)) {
				advertise = generateService.get(Long.parseLong(advId));
				position = generateService.getGeneratePositionByAdvId(Long.parseLong(advId)).get(0);
				content = generateService.getGenerateContentByAdvId(Long.parseLong(advId)).get(0);
			} else {
				advertise = new GenerateAdvertise();
				position = new GeneratePosition();
				content = new GenerateContent();
			}

			advertise.setAdvName(advName.trim());
			advertise.setAdvType(advType);
			advertise.setStartDate(EcConstant.PATTERN_DATE_FORMAT.parse(startDate));
			advertise.setEndDate(EcConstant.PATTERN_DATE_FORMAT.parse(endDate));
			advertise.setAdvStatus(advStatus);
			position.setMainCateId(Long.parseLong(mainCateId));
			if (StringUtils.equalsIgnoreCase("100_BANNER", advType)) {
				position.setChildCateId(Long.parseLong(childCateId));
			}
			position.setAdvTarget(advTarget);
			content.setContents(contents.toString());
			contents.setLength(0);
			if (StringUtils.isBlank(advId)) {
				result = generateService.saveGenerateCombine(advertise, position, content);
			} else {
				result = generateService.updateGenerateCombine(advertise, position, content);
			}
			contents.setLength(0);
			if (!result) {
				return "editCateBannerAdv";
			} else {
				search();
				return "listAdv";
			}
		}
		return "editCateBannerAdv";
	}

	/**
	 * 新增馆首页/分类页主打商品
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveCateMainProdAdv() throws Exception {
		if (cateMainProdAdvValidate()) {
			if (StringUtils.isNotBlank(advId)) {
				advertise = generateService.get(Long.parseLong(advId));
				position = generateService.getGeneratePositionByAdvId(Long.parseLong(advId)).get(0);
				content = generateService.getGenerateContentByAdvId(Long.parseLong(advId)).get(0);
			} else {
				advertise = new GenerateAdvertise();
				position = new GeneratePosition();
				content = new GenerateContent();
			}
			advertise.setAdvName(advName.trim());
			advertise.setAdvType(advType);
			advertise.setStartDate(EcConstant.PATTERN_DATE_FORMAT.parse(startDate));
			advertise.setEndDate(EcConstant.PATTERN_DATE_FORMAT.parse(endDate));
			advertise.setAdvStatus(advStatus);
			position.setMainCateId(Long.parseLong(mainCateId));
			if (StringUtils.equalsIgnoreCase("100_PROMOTION", advType)) {
				position.setChildCateId(Long.parseLong(childCateId));
			}

			position.setAdvTarget(advTarget);
			contents = new StringBuilder("");
			contents.append("tab1Title=").append(tab1Title).append(lineBreak);
			contents.append("tab1ProdId1=").append(tab1ProdId1).append(lineBreak);
			contents.append("tab1ProdId2=").append(tab1ProdId2).append(lineBreak);
			contents.append("tab1ProdId3=").append(tab1ProdId3).append(lineBreak);
			contents.append("tab2Title=").append(tab2Title).append(lineBreak);
			contents.append("tab2ProdId1=").append(tab2ProdId1).append(lineBreak);
			contents.append("tab2ProdId2=").append(tab2ProdId2).append(lineBreak);
			contents.append("tab2ProdId3=").append(tab2ProdId3).append(lineBreak);
			contents.append("tab3Title=").append(tab3Title).append(lineBreak);
			contents.append("tab3ProdId1=").append(tab3ProdId1).append(lineBreak);
			contents.append("tab3ProdId2=").append(tab3ProdId2).append(lineBreak);
			contents.append("tab3ProdId3=").append(tab3ProdId3).append(lineBreak);
			contents.append("tab4Title=").append(tab4Title).append(lineBreak);
			contents.append("tab4ProdId1=").append(tab4ProdId1).append(lineBreak);
			contents.append("tab4ProdId2=").append(tab4ProdId2).append(lineBreak);
			contents.append("tab4ProdId3=").append(tab4ProdId3).append(lineBreak);
			contents.append("tab5Title=").append(tab5Title).append(lineBreak);
			contents.append("tab5ProdId1=").append(tab5ProdId1).append(lineBreak);
			contents.append("tab5ProdId2=").append(tab5ProdId2).append(lineBreak);
			contents.append("tab5ProdId3=").append(tab5ProdId3).append(lineBreak);
			content.setContents(contents.toString());
			contents.setLength(0);
			if (StringUtils.isBlank(advId)) {
				if (!generateService.saveGenerateCombine(advertise, position, content)) {
					return editCateMainProdAdv();
				}
			} else {
				if (!generateService.updateGenerateCombine(advertise, position, content)) {
					return editCateMainProdAdv();
				}
			}
			return search();
		} else {
			return editCateMainProdAdv();
		}
	}

	EventImage saveImgFile(File imgFile, String fileName, String advType) throws Exception {
		try {
			eventImage = new EventImage();
			eventImage.setImgType(fileName.substring(fileName.lastIndexOf("."), fileName.length()));
			eventImage = generateService.saveEventImage(eventImage);
			boolean result = GenerateHelper.uploadImage(eventImage, imgFile, advType);
			if (result) {
				return eventImage;
			} else {
				return null;
			}
		} catch (Exception e) {
			logger.info("exception :{}", e.getMessage());
		}
		return null;
	}

	/**
	 * 广告列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String search() throws Exception {
		CommonCriteria crit = new CommonCriteria();
		if (StringUtils.isNotBlank(advNameParam)) {
			crit.addEq("advName", advNameParam);
		}
		if (StringUtils.isNotBlank(startDateParam)) {
			crit.addGe("startDate", startDateParam);
		}
		if (StringUtils.isNotBlank(endDateParam)) {
			crit.addLe("endDate", endDateParam);
		}
		if (StringUtils.isNotBlank(advStatusParam)) {
			crit.addEq("advStatus", advStatusParam);
		}
		if (StringUtils.isNotBlank(advTypeParam)) {
			crit.addEq("advType", advTypeParam);
		}

		int contentSize = generateService.getGenerateAdvertiseSize(crit).intValue();
		pager.setTotalSize(contentSize);

		advertises = generateService.getGenerateAdvertiseList(pager.getCurrentPage() * pager.getPageRecord(), pager
				.getPageRecord(), crit, null);
		return "listAdv";
	}

	public void setAdvBannerTagMenu(Menu advBannerTagMenu) {
		this.advBannerTagMenu = advBannerTagMenu;
	}

	public void setAdvertise(GenerateAdvertise advertise) {
		this.advertise = advertise;
	}

	public void setAdvertises(List<GenerateAdvertise> advertises) {
		this.advertises = advertises;
	}

	public void setAdvId(String advId) {
		this.advId = advId;
	}

	public void setAdvName(String advName) {
		this.advName = advName;
	}

	public void setAdvNameParam(String advNameParam) {
		this.advNameParam = advNameParam;
	}

	public void setAdvPosition(String advPosition) {
		this.advPosition = advPosition;
	}

	public void setAdvStatus(String advStatus) {
		this.advStatus = advStatus;
	}

	public void setAdvStatusMenu(Menu advStatusMenu) {
		this.advStatusMenu = advStatusMenu;
	}

	public void setAdvStatusParam(String advStatusParam) {
		this.advStatusParam = advStatusParam;
	}

	public void setAdvTarget(String advTarget) {
		this.advTarget = advTarget;
	}

	public void setAdvType(String advType) {
		this.advType = advType;
	}

	public void setAdvTypeMenu(Menu advTypeMenu) {
		this.advTypeMenu = advTypeMenu;
	}

	public void setAdvTypeParam(String advTypeParam) {
		this.advTypeParam = advTypeParam;
	}

	public void setBannerContent1(String bannerContent1) {
		this.bannerContent1 = bannerContent1;
	}

	public void setBannerContent2(String bannerContent2) {
		this.bannerContent2 = bannerContent2;
	}

	public void setBannerContent3(String bannerContent3) {
		this.bannerContent3 = bannerContent3;
	}

	public void setBannerImg(File bannerImg) {
		this.bannerImg = bannerImg;
	}

	public void setBannerImgFolder(String bannerImgFolder) {
		this.bannerImgFolder = bannerImgFolder;
	}

	public void setBannerImgLink(String bannerImgLink) {
		this.bannerImgLink = bannerImgLink;
	}

	public void setBannerImgName(String bannerImgName) {
		this.bannerImgName = bannerImgName;
	}

	public void setBannerLink1(String bannerLink1) {
		this.bannerLink1 = bannerLink1;
	}

	public void setBannerLink2(String bannerLink2) {
		this.bannerLink2 = bannerLink2;
	}

	public void setBannerLink3(String bannerLink3) {
		this.bannerLink3 = bannerLink3;
	}

	public void setBannerTag1(String bannerTag1) {
		this.bannerTag1 = bannerTag1;
	}

	public void setBannerTag2(String bannerTag2) {
		this.bannerTag2 = bannerTag2;
	}

	public void setBannerTag3(String bannerTag3) {
		this.bannerTag3 = bannerTag3;
	}

	public void setCategoryRoot(List<Category> categoryRoot) {
		this.categoryRoot = categoryRoot;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public void setChildCateId(String childCateId) {
		this.childCateId = childCateId;
	}

	public void setContent(GenerateContent content) {
		this.content = content;
	}

	public void setContents(StringBuilder contents) {
		this.contents = contents;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public void setEndDateParam(String endDateParam) {
		this.endDateParam = endDateParam;
	}

	public void setGenerateService(GenerateService generateService) {
		this.generateService = generateService;
	}

	public void setShelfService(ShelfService shelfService) {
		this.shelfService = shelfService;
	}

	public void setJsonProduct(Product jsonProduct) {
		this.jsonProduct = jsonProduct;
	}

	public void setMainCateId(String mainCateId) {
		this.mainCateId = mainCateId;
	}

	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public void setPosition(GeneratePosition position) {
		this.position = position;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public void setStartDateParam(String startDateParam) {
		this.startDateParam = startDateParam;
	}

	public void setTab1ProdId1(String tab1ProdId1) {
		this.tab1ProdId1 = tab1ProdId1;
	}

	public void setTab1ProdId2(String tab1ProdId2) {
		this.tab1ProdId2 = tab1ProdId2;
	}

	public void setTab1ProdId3(String tab1ProdId3) {
		this.tab1ProdId3 = tab1ProdId3;
	}

	public void setTab1Title(String tab1Title) {
		this.tab1Title = tab1Title;
	}

	public void setTab2ProdId1(String tab2ProdId1) {
		this.tab2ProdId1 = tab2ProdId1;
	}

	public void setTab2ProdId2(String tab2ProdId2) {
		this.tab2ProdId2 = tab2ProdId2;
	}

	public void setTab2ProdId3(String tab2ProdId3) {
		this.tab2ProdId3 = tab2ProdId3;
	}

	public void setTab2Title(String tab2Title) {
		this.tab2Title = tab2Title;
	}

	public void setTab3ProdId1(String tab3ProdId1) {
		this.tab3ProdId1 = tab3ProdId1;
	}

	public void setTab3ProdId2(String tab3ProdId2) {
		this.tab3ProdId2 = tab3ProdId2;
	}

	public void setTab3ProdId3(String tab3ProdId3) {
		this.tab3ProdId3 = tab3ProdId3;
	}

	public void setTab3Title(String tab3Title) {
		this.tab3Title = tab3Title;
	}

	public void setTab4ProdId1(String tab4ProdId1) {
		this.tab4ProdId1 = tab4ProdId1;
	}

	public void setTab4ProdId2(String tab4ProdId2) {
		this.tab4ProdId2 = tab4ProdId2;
	}

	public void setTab4ProdId3(String tab4ProdId3) {
		this.tab4ProdId3 = tab4ProdId3;
	}

	public void setTab4Title(String tab4Title) {
		this.tab4Title = tab4Title;
	}

	public void setTab5ProdId1(String tab5ProdId1) {
		this.tab5ProdId1 = tab5ProdId1;
	}

	public void setTab5ProdId2(String tab5ProdId2) {
		this.tab5ProdId2 = tab5ProdId2;
	}

	public void setTab5ProdId3(String tab5ProdId3) {
		this.tab5ProdId3 = tab5ProdId3;
	}

	public void setTab5Title(String tab5Title) {
		this.tab5Title = tab5Title;
	}
}